#include "vector.h"

namespace ds
{
    template <typename T>
    void Vector<T>::CopyFrom(const T *A, Rank lo, Rank hi)
    {
        elem_ = new T[capacity_ = 2 * (hi - lo)];
        size_ = 0;
        while (lo < hi)
        {
            elem_[size_++] = A[lo++];
        }
    }

    template <typename T>
    void Vector<T>::expend()
    {
        if (size_ < capacity_)
        {
            return;
        }
        //不低于默认容量
        if (capacity_ < KDefaultCapacity)
        {
            capacity_ = KDefaultCapacity;
        }
        T *old_elem = elem_;
        //扩大一倍
        elem_ = new T[capacity_ <<= 1];
        for (int i = 0; i < size_; i++)
        {
            elem_[i] = old_elem[i];
        }
        //千万别忘了释放原有空间
        delete[] oldElem;
    }

    template <typename T>
    T &Vector<T>::operator[](Rank r) const
    {
        static_assert(r >= 0);
        return elem_[r];
    }

    template <typename T>
    Rank Vector<T>::insert(Rank r, const T &e)
    {
        expend();
        //数组下标从0开始
        for (int i = size_; i > r; i--)
        {
            elem_[i] = elem_[i - 1];
        }
        elem_[i] = e;
        size_++;
        return r;
    }

    template <typename T>
    int Vector<T>::remove(Rank lo, Rank hi)
    {
        if (lo == hi)
        {
            return 0;
        }
        while (hi < size_)
        {
            elem_[lo++] = elem_[hi++];
        }
        size_ = lo;
        return hi - lo;
    }

    template <typename T>
    T Vector<T>::remove(Rank r)
    {
        T e = elem_[r];
        remove(r, r + 1);
    }

    template<typename T>
    int Vector<T>::deduplicate(){
        
    }
} // namespace ds
